/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MagnetoProject;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.geom.Ellipse2D;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

/**
 *
 * @author ken
 */
public class DisplayArea extends JPanel implements Runnable {

    int[] inputs = new int[3];
     static int oldX = 0;
     static int oldY = 0;
   

private static final int THRESHOLD = 20;

    //this is my constructor
    public DisplayArea(){

        setBorder(BorderFactory.createLineBorder(new java.awt.Color(22, 174, 212)));
        setBackground(Color.white);

    }

    public void run (){

        

       while(true){
        
        while(Buffer.haveData()){
        
            //setData(true);
            dataReceived();
        
        }

            
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                System.out.println("something woke me up from sleep!");
            }


       }


   }


    public void something(StorageEvent event){
        System.out.println("Hello world");

    }

    public void dataReceived() {

       int curX;
       int curY;
       int magnitude;
       int diffY;
       int diffX;
      
       Graphics g = MagnetoMeter.displayArea.getGraphics();
       Graphics2D g2D = ( Graphics2D ) g;

        try{
        //System.out.println("Display Area: I got some data!")
        inputs = (int[]) Buffer.remove();
 

        //draw a line from oldX, oldY, to the new x and y
        curX = inputs[0];
        curY = inputs[1];
        magnitude = inputs[2];

        diffX = inputs[0] - oldX;
        diffY = inputs[1] - oldY;
        
        diffX = absolute(diffX);
        diffY = absolute(diffY);


    if(g != null){
    //System.out.println("g exists!!");

        g.drawLine(oldX, oldY, curX, curY);
        drawShape(g2D,magnitude,curX,curY);


    }
        
        oldX = inputs[0];
        oldY = inputs[1];
        
       }catch(NullPointerException e){
           System.out.println("You have a null pointer exception");
       }


    }

       private int absolute(int x){

        if (x < 0)
            x = -x;
        return x;
    }

    private void drawShape(Graphics2D g2d, int magnitude, int x, int y){

        int width = magnitude;
        int height = magnitude;


        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      //  g2d.setStroke(new BasicStroke(2));
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f));
        g2d.setColor(Color.RED);
        g2d.fillOval(x-magnitude/2,y-magnitude/2,width,height);
        
        
        
        
        /*
        // draw 2D ellipse filled with a blue-yellow gradient
         g2d.setPaint( new GradientPaint( 5, 30, // x1, y1
         Color.blue, // initial Color
         35, 100, // x2, y2
         Color.yellow, // end Color
         true ) ); // cyclic
         g2d.fill( new Ellipse2D.Double( 5, 30, 65, 100 ) );
*/


    }




    
 }
